﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Pages
{
    public partial class OrderList : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {               
                if (base.IsVerified)
                {
                    BindData();
                    BindStatistics();
                }
                else
                {
                    Response.Redirect(URL_INDEX);
                }

            }

            //条件查询
            if (!string.IsNullOrEmpty(hidStr.Value))
            {
                BindData();
            }

        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            Model.NetPager pager = new Model.NetPager();
            pager.PageIndex = AspNetPager1.CurrentPageIndex;
            pager.TableName = "OrderInfo";
            pager.PrimaryKey = "OrderID";

            string whereStr = "Seller='" + base.VerifiedUserID + "' and State = " + Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled) + " and IsDel = " + Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel);

            int statusId = 0;
            switch (hidStr.Value)
            {
                case "0":                                                                    //已付款
                    statusId = Convert.ToInt32(AUV5.Common.Enum.OrderState.Payment);
                    whereStr += " and StatusID =" + statusId;
                    break;
                case "1":                                                                    //未付款
                    statusId = Convert.ToInt32(AUV5.Common.Enum.OrderState.Default);
                    whereStr += " and StatusID =" + statusId;
                    break;
                case "2":                                                                    //己发货
                    statusId = Convert.ToInt32(AUV5.Common.Enum.OrderState.Shipped);
                    whereStr += " and StatusID =" + statusId;
                    break;
                case "3":                                                                   //己完成和关闭之间
                    statusId = Convert.ToInt32(AUV5.Common.Enum.OrderState.Completed);
                    whereStr += " and StatusID >= " + statusId + " and StatusID < " + Convert.ToInt32(AUV5.Common.Enum.OrderState.Cloesd);
                    break;
                case "4":
                    statusId = Convert.ToInt32(AUV5.Common.Enum.OrderState.Cloesd);
                    whereStr += " and StatusID >= " + statusId;
                    break;
                case "8":                                                                  //己完成                   
                    whereStr += " and StatusID =" + Convert.ToInt32(AUV5.Common.Enum.OrderState.Completed) + " or StatusID = " + Convert.ToInt32(AUV5.Common.Enum.OrderState.SysCompleted);
                    break;
                case "9":                                                                  //已评价
                    statusId = Convert.ToInt32(AUV5.Common.Enum.OrderState.HasEvaluation);
                    whereStr += " and StatusID =" + statusId;
                    break;
                case "10":                                                                 //已互评
                    statusId = Convert.ToInt32(AUV5.Common.Enum.OrderState.HasMutualEvaluation);
                    whereStr += " and StatusID =" + statusId;
                    break;
                case "15":                                                                  //已关闭
                    statusId = Convert.ToInt32(AUV5.Common.Enum.OrderState.Cloesd);
                    whereStr += " and StatusID =" + statusId;
                    break;
                case "16":                                                                 //买家强制关闭
                    statusId = Convert.ToInt32(AUV5.Common.Enum.OrderState.BuyerForceClosed);
                    whereStr += " and StatusID =" + statusId;
                    break;
                case "17":                                                                  //卖家强制关闭
                    statusId = Convert.ToInt32(AUV5.Common.Enum.OrderState.SellerForceClosed);
                    whereStr += " and StatusID =" + statusId;
                    break;           
                default:
                    statusId = Convert.ToInt32(AUV5.Common.Enum.OrderState.Payment);
                    whereStr += " and StatusID =" + statusId;
                    break;
            }

            pager.Where = whereStr;
            pager.Order = "CreateDate desc,OrderID desc";
            pager.SortType = (int)Common.Enum.SortType.Multi;
            pager.PageSize = AspNetPager1.PageSize;

            rpOrderList.DataSource = AUV5.BLL.OrderInfoBLL.Instance.GetOrderListPage(pager);
            rpOrderList.DataBind();
            AspNetPager1.RecordCount = pager.TotalCount;
        }

        /// <summary>
        /// 绑定统计数据
        /// 创建人：Wilson
        /// 创建人：2011-11-27
        /// </summary>
        private void BindStatistics()
        {
            string userId = base.VerifiedUserID;

            lblDefaultCount.Text = Convert.ToString(AUV5.BLL.OrderInfoBLL.Instance.GetDefaultOrderInfoCountBySeller(userId));   //未付款订单数

            lblPaymentCount.Text = Convert.ToString(AUV5.BLL.OrderInfoBLL.Instance.GetPaymentOrderInfoCountBySeller(userId));   //已付款订单数

            lblShippedCount.Text = Convert.ToString(AUV5.BLL.OrderInfoBLL.Instance.GetShippedOrderInfoCountBySeller(userId));   //已发货订单数

            lblCompletedCount.Text = Convert.ToString(AUV5.BLL.OrderInfoBLL.Instance.GetSellerCompleteBySeller(userId));  //己完成订单数

            lblClosedCount.Text = Convert.ToString(AUV5.BLL.OrderInfoBLL.Instance.GetClosedOrderCountBySeller(userId));         //已关闭的订单数
            

            
        }

        protected void rpOrderList_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Label lblId = e.Item.FindControl("lblId") as Label;   //主键
                LinkButton lbtnServiceTitle = e.Item.FindControl("lbtnServiceTitle") as LinkButton;   //服务标题
                LinkButton lbtnBuyer = e.Item.FindControl("lbtnBuyer") as LinkButton;                    //购买人
                Label lblStatus = e.Item.FindControl("lblStatus") as Label;             //状态
                Image imgUser = e.Item.FindControl("imgUser") as Image;                //头像
                LinkButton lbtnContract = e.Item.FindControl("lbtnContract") as LinkButton;   //联系
                LinkButton lblEvaluate = e.Item.FindControl("lblEvaluate") as LinkButton;   //评价
                LinkButton lbtnDetail = e.Item.FindControl("lbtnDetail") as LinkButton;  //查看详情
                    
                //-------------------------------订单-----------------------------------------
                AUV5.Model.OrderInfo info = AUV5.BLL.OrderInfoBLL.Instance.GetDetail(lblId.Text);

                if (info != null)
                {                  
                    lbtnDetail.PostBackUrl = "OrderDetail.aspx?id=" + info.OrderID;
                       
                    lblEvaluate.PostBackUrl = "OrderDetail.aspx?id=" + info.OrderID;

                    switch (info.StatusID)
                    {
                        case (int)AUV5.Common.Enum.OrderState.Default:
                            lblStatus.CssClass = "listststus listgray";
                            lblStatus.Text = AUV5.Common.PageBase.GetEnumDesc(typeof(AUV5.Common.Enum.OrderState), Convert.ToInt32(info.StatusID));
                            lbtnDetail.Visible = false;
                            lbtnContract.CssClass = "orderlinedel";
                            break;
                        case (int)AUV5.Common.Enum.OrderState.Payment:
                            lblStatus.CssClass = "listststus listgreen";
                            lblStatus.Text = AUV5.Common.PageBase.GetEnumDesc(typeof(AUV5.Common.Enum.OrderState), Convert.ToInt32(info.StatusID));
                            break;
                        case (int)AUV5.Common.Enum.OrderState.Shipped:
                            lblStatus.CssClass = "listststus listyellow";
                            lblStatus.Text = AUV5.Common.PageBase.GetEnumDesc(typeof(AUV5.Common.Enum.OrderState), Convert.ToInt32(info.StatusID));
                            break;
                        case (int)AUV5.Common.Enum.OrderState.Completed:
                            lblStatus.CssClass = "listststus listapp1";
                            lblEvaluate.Visible = true;
                            lblStatus.Text = "未评价";
                            break;
                        case (int)AUV5.Common.Enum.OrderState.HasEvaluation:
                            lblStatus.CssClass = "listststus listapp2";
                            lblEvaluate.Visible = true;
                            lblStatus.Text = AUV5.Common.PageBase.GetEnumDesc(typeof(AUV5.Common.Enum.OrderState), Convert.ToInt32(info.StatusID));
                            break;
                        case (int)AUV5.Common.Enum.OrderState.HasMutualEvaluation:
                            lblStatus.CssClass = "listststus listapp3";
                            lblEvaluate.Visible = true;
                            lblStatus.Text = AUV5.Common.PageBase.GetEnumDesc(typeof(AUV5.Common.Enum.OrderState), Convert.ToInt32(info.StatusID));
                            break;
                        case (int)AUV5.Common.Enum.OrderState.Cloesd:
                            lblStatus.CssClass = "listststus listclose1";
                            lblStatus.Text = AUV5.Common.PageBase.GetEnumDesc(typeof(AUV5.Common.Enum.OrderState), Convert.ToInt32(info.StatusID));                           
                            break;
                        case (int)AUV5.Common.Enum.OrderState.SellerForceClosed:
                            lblStatus.CssClass = "listststus listclose2";
                            lblStatus.Text = AUV5.Common.PageBase.GetEnumDesc(typeof(AUV5.Common.Enum.OrderState), Convert.ToInt32(info.StatusID));                           
                            break;
                        case (int)AUV5.Common.Enum.OrderState.BuyerForceClosed:
                            lblStatus.CssClass = "listststus listclose3";
                            lblStatus.Text = AUV5.Common.PageBase.GetEnumDesc(typeof(AUV5.Common.Enum.OrderState), Convert.ToInt32(info.StatusID));                            
                            break;
                        default:
                            break;
                    }


                    //------------------------------订单项---------------------------------------------------
                    AUV5.Model.OrderItem orderItem = AUV5.BLL.OrderItemBLL.Instance.GetOrderItemByOrderID(lblId.Text);

                    string contractUrl = string.Empty;  //联系URL

                    if (orderItem != null)
                    {
                        AUV5.Model.ServiceInfo service = AUV5.BLL.ServiceInfoBLL.Instance.GetDetail(orderItem.ServiceID);

                        if (service != null)
                        {
                            contractUrl = "/Pages/MessageShow.aspx?userID=" + info.Buyer + "&serviceID=" + service.ServiceID;
                            lbtnServiceTitle.Text = service.Title;
                            lbtnServiceTitle.PostBackUrl = "/Pages/ServiceDetail.aspx?serviceID=" + service.ServiceID;
                            lbtnBuyer.PostBackUrl = contractUrl;
                            lbtnContract.PostBackUrl = contractUrl;                 
                        }
                    }

                    //------------------------------买家-------------------------------------------
                    AUV5.Model.UserInfo user = AUV5.BLL.UserInfoBLL.Instance.GetDetail(info.Buyer);

                    if (user != null)
                    {
                        lbtnBuyer.Text = user.UserName;
                        imgUser.ImageUrl = user.PhotoPath;
                    }
                }

            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}